﻿//-----------------------------------------------------------------------
// <copyright file="MvcLog.cs" company="SLWY Enterprises">
// * Copyright (C) 2017 四川商旅无忧科技有限公司 版权所有。
// * version : 1.0
// * author  : qinchaoyue
// * FileName: SystemLog.cs
// * history : created by qinchaoyue 2017-11-21 05:17:18
// </copyright>
//-----------------------------------------------------------------------

namespace Slwy.Mvc.Logging
{
    using System;

    using Microsoft.Extensions.DependencyInjection;

    using Slwy.Context;
    using Slwy.FrameCore.Dependency;
    using Slwy.Logger.Contracts;
    using Slwy.Logger.Models;
    using Slwy.Utility.DebugLog;

    /// <summary>
    /// 系统日志记录者
    /// </summary>
    public static class MvcLog
    {
        /// <summary>
        /// 记录异常日志
        /// </summary>
        /// <param name="ex">异常</param>
        public static void Exception(Exception ex)
        {
            try
            {
                var logger = ServiceLocator.Instance.GetService<ILogger>();
                var exModel = new ExceptionModel
                {
                    Context = ex.ToString(),
                    ErrorCode = string.Empty,
                    ErrorLevel = "Error",
                    Message = ex.Message
                };
                var exceptionId = new object();
                if (SlwyContext.Current.TryGetValue($"AutoDelete_{SlwyContext.Current.Operator.KeyId}_ExceptionId", out exceptionId))
                {
                    SlwyContext.Current[$"AutoDelete_{SlwyContext.Current.Operator.KeyId}_ExceptionId"] = $"{exceptionId}, {exModel.Id}";
                }
                else
                {
                    SlwyContext.Current[$"AutoDelete_{SlwyContext.Current.Operator.KeyId}_ExceptionId"] = $"{exModel.Id}";
                }

                logger.Error(exModel);
            }
            catch (Exception e)
            {
                Logs.Err(e.ToString());
            }
        }
    }
}
